#include <iostream>
#include <limits>

using std::cout;
using std::endl;
using std::cin;
using std::istream;

void readInteger(istream & is)
{
    cout << "please input a valid integer:" << endl;
    int num;
    //逗号表达式: 将最后一个表达式的值作为整个表达式的值
    while(is >> num, !is.eof()) {
        if(is.bad()) {
            //不可恢复的错误
            cout << "stream has broken." << endl;
            return;
        } else if(is.fail()) {
            is.clear();
            is.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            cout << "please input a valid integer:" << endl;
        } else {
            cout << "num:" << num << endl;
            break;
        }
    }
}

int main()
{
    readInteger(cin);
    return 0;
}

